#include "CAnimation.h"
 
CAnimation::CAnimation()
{
    CurrentFrame = 0;
    MaxFrames = 0;
    FrameInc = 1;
 
    FrameRate = 100; //Milliseconds
    OldTime = 0;
 
    Oscillate = false;
    OneTimeAnimation = false;
    OneTimeCallback = NULL;
}
 
void CAnimation::OnAnimate()
{
    if(OldTime + FrameRate > SDL_GetTicks())
    {
        return;
    }
 
    OldTime = SDL_GetTicks();
 
    CurrentFrame += FrameInc;
 
    if(Oscillate)
    {
        if(FrameInc > 0)
        {
            if(CurrentFrame >= MaxFrames)
            {
                FrameInc = -FrameInc;
            }
        }
        else
        {
            if(CurrentFrame <= 0)
            {
                FrameInc = -FrameInc;
            }
        }
    }
    else
    {
        if(CurrentFrame >= MaxFrames)
        {
            if(OneTimeAnimation==true)
            {    
                *OneTimeCallback=true;
            }
                
            CurrentFrame = 0;
        }
    }
}
 
void CAnimation::SetFrameRate(int Rate)
{
    FrameRate = Rate;
}
 
void CAnimation::SetCurrentFrame(int Frame)
{
    if(Frame < 0 || Frame >= MaxFrames)
    {
        return;
    }
 
    CurrentFrame = Frame;
}
 
int CAnimation::GetCurrentFrame()
{
    return CurrentFrame;
}

void CAnimation::SetOneTimeAnimation(bool* input)
{
    OneTimeAnimation = true;
    Oscillate = false;
    OneTimeCallback = input;
}